package com.imti.mall.sale.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.sms.pojo.SmsHomeBrand;
import com.imti.mall.model.sms.pojo.SmsHomeNewProduct;
import com.imti.mall.model.sms.vo.SmsHomeBrandVo;
import com.imti.mall.model.sms.vo.SmsHomeNewProductPicVo;
import com.imti.mall.model.sms.vo.SmsHomeNewProductVo;
import com.imti.mall.sale.service.SmsHomeBrandService;
import com.imti.mall.sale.service.SmsHomeNewProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/sale")
@Api(tags = "新品推荐商品")
public class SmsHomeNewProductController {

    @Autowired
    SmsHomeNewProductService smsHomeNewProductService;

    /**
     * 查询新品推荐商品
     */
    @ApiOperation(value = "查询新品推荐商品")
    @PostMapping(value = "/findSmsHomeNewProduct" )
    public CommonResult findSmsHomeNewProduct
    ( SmsHomeNewProduct smsHomeNewProduct,
      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SmsHomeNewProduct> list = smsHomeNewProductService.findSmsHomeNewProduct(smsHomeNewProduct,pageSize, pageNum);
        if(list.size()>0){
            return CommonResult.success(CommonPage.restPage(list));
        }else {
            return CommonResult.success("未查询到信息");
        }
    }

    /**
     * 添加新品推荐商品
     */
    @ApiOperation(value = "添加新品推荐商品")
    @PostMapping(value = "/addSmsHomeNewProduct" )
    public CommonResult addSmsHomeNewProduct(@RequestBody List<SmsHomeNewProduct> smsHomeNewProduct) {
       Integer res=0;
        for(int i=0;i<smsHomeNewProduct.size();i++){
            res= smsHomeNewProductService.addSmsHomeNewProduct(smsHomeNewProduct.get(i));
        }
        if(res>0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 修改新品推荐商品
     */
    @ApiOperation(value = "修改新品推荐商品")
    @PostMapping(value = "/updateSmsHomeNewProduct" )
    public CommonResult updateSmsHomeNewProduct(SmsHomeNewProductVo smsHomeNewProductVo) {
            int res = 0;
            if (smsHomeNewProductVo.getId() != null) {
                SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductVo;
                res = smsHomeNewProductService.updateSmsHomeNewProduct(smsHomeNewProduct);
            } else {
                Long[] ids = smsHomeNewProductVo.getIds();
                for (int i = 0; i < ids.length; i++) {
                    SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductVo;
                    smsHomeNewProduct.setId(ids[i]);
                    res = smsHomeNewProductService.updateSmsHomeNewProduct(smsHomeNewProduct);
                }
            }
            if (res > 0) {
                return CommonResult.success("修改成功");
            }
            return CommonResult.failed("修改失败");
        }
    

    /**
     * 删除新品推荐商品
     */
    @ApiOperation(value = "删除新品推荐商品")
    @PostMapping(value = "/delSmsHomeNewProduct" )
    public CommonResult delSmsHomeNewProduct( SmsHomeNewProductVo smsHomeNewProductVo) {
        Long[] ids= smsHomeNewProductVo.getIds();
        int res =0;
        for(int i=0;i<ids.length;i++){
            res = smsHomeNewProductService.delSmsHomeNewProduct(ids[i].intValue());
        }
        if(res>0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }
    /**
     * 查询图片信息
     * @param smsHomeNewProductPicVo
     * @return
     */
    @ApiOperation(value = "App新品推荐图片价价位")
    @PostMapping(value = "/findSmsHomeNewProductPic" )
    public CommonResult findSmsHomeNewProductPic(SmsHomeNewProductPicVo smsHomeNewProductPicVo) {
        List<SmsHomeNewProductPicVo> list = smsHomeNewProductService.findSmsHomeNewProductPic(smsHomeNewProductPicVo);
        return CommonResult.success(list);
    }

}
